<template>
  <div>
    <div class="app-container">
      <div class="header">{{$route.meta.title}}</div>
    </div>

    <div class="app-container margin-content padding-content">
      <el-form
        label-position="left"
        :model="dataForm"
        ref="dataForm"
        label-width="120px">

        <el-row :gutter="48">
          <el-col :sm="24" :md="8">
            <el-form-item label="提运单号" prop="blNo">
              <el-input placeholder="提运单号" v-model="dataForm.blNo" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8">
            <el-form-item label="航次号" prop="voyageNo">
              <el-input placeholder="航次号" v-model="dataForm.voyageNo" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8">
            <el-form-item label="散货作业时间" prop="time">
              <el-date-picker
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                unlink-panels
                value-format="yyyy-MM-dd"
                v-model="dataForm.time"
                :picker-options="pickerOptions"
                style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <div v-show="this.toggleFlag">
          <el-row :gutter="48">
            <el-col :sm="24" :md="8" v-if="isAdmin">
              <el-form-item label="所属码头" prop="portCode">
                <treeselect
                  placeholder="请选择"
                  :options="options"
                  :disable-branch-nodes="true"
                  value-consists-of="LEAF_PRIORITY"
                  :close-on-select="true"
                  :open-on-click="true"
                  v-model="dataForm.portCode"/>
              </el-form-item>
            </el-col>
            <el-col :sm="24" :md="8">
              <el-form-item label="进出口标记" prop="ieFlag">
                <el-select filterable clearable placeholder="请选择" v-model="dataForm.ieFlag" style="width: 100%;">
                  <el-option
                    v-for="item in ieFlags"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :sm="24" :md="8">
              <el-form-item label="散货作业类型" prop="actType">
                <el-select filterable clearable placeholder="请选择" v-model="dataForm.actType" style="width: 100%;">
                  <el-option
                    v-for="item in bulkActType"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :sm="24" :md="8">
              <el-form-item label="船舶编号" prop="vesselNo">
                <el-input placeholder="船舶编号" v-model="dataForm.vesselNo" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :sm="24" :md="8">
              <el-form-item label="货物名称" prop="cargoName">
                <el-input placeholder="货物名称" v-model="dataForm.cargoName" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :sm="24" :md="8">
              <el-form-item label="发货人" prop="shipper">
                <el-input placeholder="发货人" v-model="dataForm.shipper" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <div style="text-align: right">
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
          <el-button type="text" @click="toggleSearch">
            {{toggleText}}
            <span :class="toggleIconClass"></span>
          </el-button>
        </div>

      </el-form>
    </div>

    <div class="app-container margin-content padding-content">
      <el-table
        :key="tableKey"
        :data="tableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        style="width: 100%;"
        :max-height="scrollHeight">

        <el-table-column label="提单号" property="blNo" width="130" fixed="left"></el-table-column>
        <el-table-column label="船舶编号" property="vesselNo" width="120"></el-table-column>
        <el-table-column label="航次号" property="voyageNo" width="120"></el-table-column>
        <el-table-column label="进出口类型" property="ieFlag">
          <template slot-scope="scope">
            {{scope.row.ieFlag | ieFlagFilter}}
          </template>
        </el-table-column>
        <el-table-column label="仓位" property="yardCode"></el-table-column>
        <el-table-column label="货物名称" property="cargoName"></el-table-column>
        <el-table-column label="作业类型" property="actType" width="120">
          <template slot-scope="scope">
            {{scope.row.actType | bulkTypeFilter}}
          </template>
        </el-table-column>
        <el-table-column label="作业时间" property="actTimeDesc" width="150"></el-table-column>
        <el-table-column label="操作" fixed="right" width="200">
          <template slot-scope="scope">
            <a @click="history(scope.row.blNo)">历史记录</a>
            <divider type="vertical"></divider>
            <a @click="tracking(scope.row.blNo, 2)">货物跟踪</a>
            <divider type="vertical"></divider>
            <a
              @click="view({id: scope.row.id, beginTime: scope.row.actTimeDesc, endTime: scope.row.actTimeDesc})">明细</a>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container align-right" v-show="tableData.length > 0">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="dataForm.pageNum"
          :page-size="dataForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>

    </div>

    <!-- 弹窗, 历史记录 -->
    <historical-records v-if="historyVisible" ref="historyRecords"></historical-records>
    <!-- 弹窗, 路由跟踪 -->
    <cont-track-dialog v-if="contTrackDialogVisible" ref="contTrackDialog" track-type="1"></cont-track-dialog>
  </div>
</template>

<script>
  // api
  import { fetchList } from '@/api/portBulk'
  import { getPortTree } from '@/api/port'
  // utils
  import { pickerOptions, getLately } from '@/utils'
  import { portCode, ieFlags, bulkActType } from '@/utils/selecterOptions'
  import { modifyObj } from '@/utils/tools'
  // vuex
  import { mapMutations, mapGetters } from 'vuex'
  // components
  import Divider from '@/components/Divider'
  import HistoricalRecords from './components/historical-records'
  import ContTrackDialog from '@/components/ContTrackDialog'
  import Treeselect from '@riophae/vue-treeselect'

  const time = getLately(7)

  export default {
    name: 'port-bulk-list',
    data() {
      return {
        tableKey: 0,
        tableData: [],
        loading: false,
        scrollHeight: null,
        dataForm: {
          time: time,
          portCode: null,
          blNo: '',
          containerId: '',
          ieFlag: '',
          actType: '',
          vesselNo: '',
          voyageNo: '',
          cargoName: '',
          shipper: '',
          pageNum: 1,
          pageSize: 20
        },
        total: 0,
        toggleFlag: false,
        pickerOptions: {
          shortcuts: pickerOptions
        },
        ieFlags: ieFlags,
        bulkActType: bulkActType,
        historyVisible: false,
        contTrackDialogVisible: false,
        options: []
      }
    },
    computed: {
      toggleText() {
        return this.toggleFlag ? '收起' : '展开'
      },
      toggleIconClass() {
        if (this.toggleFlag) {
          this.tableResize(570)
        } else {
          this.tableResize(420)
        }
        return this.toggleFlag ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
      },
      ...mapGetters([
        'isAdmin'
      ])
    },
    created() {
      this._getPortTree()
      this._fetchList()
      this.tableResize(420)
    },
    methods: {
      tableResize(h){
        const _height = h
        this.scrollHeight = document.body.scrollHeight - _height
        window.onresize = () => {
          this.scrollHeight = document.body.scrollHeight - _height
        }
      },
      _getPortTree() {
        getPortTree().then((res) => {
          this.options = modifyObj(res.data.data)
        })
      },
      _fetchList() {
        this.loading = true

        const [beginTime, endTime] = this.dataForm.time || ''
        const params = {beginTime, endTime}
        const data = Object.assign({}, params, this.dataForm)

        fetchList(data).then((res) => {
          this.tableData = res.data.data.dataList
          this.total = res.data.data.total
          this.loading = false
        })
      },
      history(blNo) {
        this.historyVisible = true
        this.$nextTick(() => {
          this.setCurrentId(blNo)
          this.$refs['historyRecords'].init()
        })
      },
      tracking(blNo, type) {
        this.contTrackDialogVisible = true
        this.$nextTick(() => {
          this.setCurrentId({ blNo, type })
          this.$refs['contTrackDialog'].init()
        })
      },
      view({id, beginTime, endTime}) {
        this.$parent.changeView(1)
        this.setCurrentId({id, beginTime, endTime})
      },
      resetForm() {
        this.$refs['dataForm'].resetFields()
      },
      handleSearch() {
        this.dataForm.pageNum = 1
        this._fetchList()
      },
      toggleSearch() {
        this.toggleFlag = !this.toggleFlag
      },
      handleSizeChange(val) {
        this.dataForm.pageSize = val
        this._fetchList()
      },
      handleCurrentChange(val) {
        this.dataForm.pageNum = val
        this._fetchList()
      },
      ...mapMutations({
        setCurrentId: 'SET_CURRENTID'
      })
    },
    components: {
      Divider,
      HistoricalRecords,
      ContTrackDialog,
      Treeselect
    }
  }
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" rel="stylesheet/scss" type="text/scss">

</style>
